//
//  OAuthViewController.m
//  Weibo
//
//  Created by Anasue on 9/13/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "OAuthViewController.h"
#import "MBProgressHUD+MJ.h"
#import "AFNetworking.h"
#import "TabBarViewController.h"
#import "NewFeatureViewController.h"
#import "ControllerTool.h"
#import "Account.h"
#import "AccountTool.h"


@interface OAuthViewController ()<UIWebViewDelegate>

@end

@implementation OAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建UIWebView
    UIWebView *webView = [[UIWebView alloc]init];
    webView.frame = self.view.bounds;
    [self.view addSubview:webView];
    
    //加载登录页面
    NSURL *url = [NSURL URLWithString:@"https://api.weibo.com/oauth2/authorize?client_id=3398804718&redirect_uri=http://www.baidu.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    
    //设置代理
    webView.delegate = self;
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showMessage:@"正在加载中..."];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUD];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUD];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //获得请求地址
    NSString *url = request.URL.absoluteString;
    //判断URL是否为回调地址
//    url：http://www.baidu.com/?code=36ce25bc172ce541ef08267bd396e8f9
//    Log(@"%@",url.absoluteString);
    
    NSRange range = [url rangeOfString:@"http://www.baidu.com/?code="];
    if (range.location != NSNotFound) {
        //截取授权成功后的请求标记
        int from = range.location + range.length;
        NSString *code = [url substringFromIndex:from];
        // 根据code获得一个accessToken
        [self accessTokenWithCode:code];
        // 禁止加载回调页面
        return NO;
    }
    return YES;
}

- (void)accessTokenWithCode:(NSString *)code
{
    //1.封装请求管理者
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    //2.封装请求参数
    NSMutableDictionary *paras = [NSMutableDictionary dictionary];
    paras[@"client_id"] = @"3398804718";
    paras[@"client_secret"] = @"b96631b30ece2f260f2fc70b354ded0d";
    paras[@"grant_type"] = @"authorization_code";
    paras[@"code"] = code;
    paras[@"redirect_uri"] = @"http://www.baidu.com";
    //3.发送请求
    [mgr POST:@"https://api.weibo.com/oauth2/access_token" parameters:paras
    success:^(AFHTTPRequestOperation *operation, NSDictionary *accountDict) {
        [MBProgressHUD hideHUD];
        Log(@"请求成功--%@", accountDict);
        Account *account = [Account accountWithDict:accountDict];
        [AccountTool save:account];
        [ControllerTool chooseRootController];
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        Log(@"请求失败--%@", error);
    }];
    
    
}

@end
